﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using XinYi.CrookedShow.BLL;
using XinYi.CrookedShowBack.Models;
using XinYi.CrookedShowMode;

namespace XinYi.CrookedShowBack.Controllers
{
    public class OrderController : Controller
    {
        OrderService orderService = new OrderService();
        /// <summary>
        /// 批量删除根据Id
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <returns></returns>
        public JsonResult BatchDeleteById(string id)
        {
            //用string类型数组去接收所传参数拆分后的值
            string[] num1 = id.Split(',');
            //将num1中的值用int类型数组去接收
            int[] num2 = new int[num1.Length];
            for (int i = 0; i < num2.Length; i++)
            {
                num2[i] = Convert.ToInt32(num1[i]);
            }
            //定义List集合
            List<Order> orderinfoList = new List<Order>();
            List<Order_Detail> orderdetailList = new List<Order_Detail>();
            BaseService<Order_Detail> _orderdetailbaseService = new BaseService<Order_Detail>();
            //循环获取对象
            for (int i = 0; i < num2.Length; i++)
            {
                int num = num2[i];
                //定义查询拉姆达表达式
                Expression<Func<Order, bool>> orderinfoLambda = a => a.Order_Id == num;
                Expression<Func<Order_Detail, bool>> orderdetailLambda = a => a.Order_Id == num;
                //循环查出每一个对象存入List集合
                
                orderinfoList.Add(orderService.Query(orderinfoLambda));
                orderdetailList.Add(_orderdetailbaseService.Query(orderdetailLambda));
            }
            //创建操作类对象 
            OperateResult operateResult = new OperateResult();
            //调用批量删除方法
            operateResult.Success = orderService.BatchDeleteById(orderinfoList, orderdetailList);
            return Json(operateResult, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 删除功能
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult Delete(int id)
        {
            //创建操作类对象 
            OperateResult operateResult = new OperateResult();
            //创建业务层对象
            BaseService<Order> orderinfo = new BaseService<Order>();
            BaseService<Order_Detail> orderdetail = new BaseService<Order_Detail>();
            //定义查询拉姆达表达式
            Expression<Func<Order, bool>> orderinfoLambda = a => a.Order_Id == id;
            Expression<Func<Order_Detail, bool>> orderdetailLambda = a => a.Order_Id == id;
            //调用查询返回一个对象
            Order orderinfoList = orderinfo.Query(orderinfoLambda);
            List<Order_Detail> orderdetailList = orderdetail.QueryList(orderdetailLambda);
            //调用删除方法
            operateResult.Success = orderService.Delete(orderinfoList, orderdetailList);
            return Json(operateResult);
        }
        /// <summary>
        /// 修改发货状态
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <returns></returns>
        public JsonResult UpdateStatus(OrderInformation orderQuery)
        {
            //创建操作类对象 
            OperateResult operateResult = new OperateResult();
            //定义查询拉姆达表达式
            DateTime d= DateTime.Now;
            Expression<Func<Order, bool>> orderinfoLambda = a => a.Order_Id == orderQuery.Order_Id;
            orderQuery.CreateTime = DateTime.Now;
            //调用查询返回一个对象
            Order orderinfoList = orderService.Query(orderinfoLambda);
            //修改状态
            orderinfoList.Order_Status = Convert.ToByte(orderQuery.Order_Status);
            //调用修改方法
            operateResult.Success = orderService.Update(orderinfoList);
            return Json(operateResult, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 交易统计--折线图
        /// </summary>
        /// <returns></returns>
        public JsonResult QueryInfo()
        {
            //查询表达式
            Expression<Func<Order, bool>> orderinfoLambda = a => true;
            //调用查询方法返回一个集合
            List<Order> orderInfos = orderService.QueryList(orderinfoLambda);
            //按日期进行分组来得到每个时间段的交易金额和订单数量并转化为List集合
            var q = orderInfos.GroupBy(a => a.Shipping_Time.Value.Date).Select(a => new
            {
                count = a.Count(),
                money = a.Sum(b => b.Order_Money)

            }).ToList();
            //定义交易金额数组
            int[] ids = new int[12];
            //定义订单数量数组
            int[] idss = new int[12];
            //循环遍历
            for (int i = 0; i < q.Count; i++)
            {
                ids[i] = Convert.ToInt32(q[i].money);
                idss[i] = q[i].count;
            }
            //创建以int类型的数组为对象集合
            List<int[]> vs = new List<int[]>();
            //将交易金额添加到集合
            vs.Add(ids);
            //将订单数量添加到集合
            vs.Add(idss);
            return Json(vs, JsonRequestBehavior.AllowGet);
        }
    }
}